package com.dzt.androidframework.activity;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;

import com.dzt.androidframework.R;

public class PlaybackService extends Service {
	private static final String ACTION_PLAY_TOGGLE = "com.dzt.music.ACTION.PLAY_TOGGLE";
	private static final String ACTION_PLAY_LAST = "com.dzt.music.ACTION.PLAY_LAST";
	private static final String ACTION_PLAY_NEXT = "com.dzt.music.ACTION.PLAY_NEXT";
	private static final String ACTION_STOP_SERVICE = "com.dzt.music.ACTION.STOP_SERVICE";

	private static final int NOTIFICATION_ID = 1;
	private RemoteViews mContentViewBig, mContentViewSmall;

	public PlaybackService() {
	}

	private final Binder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public PlaybackService getService() {
			return PlaybackService.this;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		showNotification();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			String action = intent.getAction();
			if (ACTION_PLAY_TOGGLE.equals(action)) {

			} else if (ACTION_PLAY_NEXT.equals(action)) {

			} else if (ACTION_PLAY_LAST.equals(action)) {

			} else if (ACTION_STOP_SERVICE.equals(action)) {
				stopForeground(true);
			}
		}
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}


	@Override
	public boolean stopService(Intent name) {
		stopForeground(true);
		return super.stopService(name);
	}

	/**
	 * Show a notification while this service is running.
	 */
	private void showNotification() {
		// The PendingIntent to launch our activity if the user selects this notification
		//PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SplashActivity.class), 0);
		Intent hudIntent = new Intent(this, SplashActivity.class);
		hudIntent.setAction(Intent.ACTION_MAIN);
		hudIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		hudIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, hudIntent, 0);
		// Set the info for the views that show in the notification panel.
		Notification notification = new NotificationCompat.Builder(this)
				.setSmallIcon(R.mipmap.ic_notification_app_logo)  // the status icon
				.setWhen(System.currentTimeMillis())  // the time stamp
				.setContentIntent(contentIntent)  // The intent to send when the entry is clicked
				.setCustomContentView(getSmallContentView())
				.setCustomBigContentView(getBigContentView())
				.setPriority(NotificationCompat.PRIORITY_MAX)
				.setOngoing(true)
				.build();

		// Send the notification.
		startForeground(NOTIFICATION_ID, notification);
	}

	private RemoteViews getSmallContentView() {
		if (mContentViewSmall == null) {
			mContentViewSmall = new RemoteViews(getPackageName(), R.layout.remote_view_music_player_small);
			setUpRemoteView(mContentViewSmall);
		}
		updateRemoteViews(mContentViewSmall);
		return mContentViewSmall;
	}

	private RemoteViews getBigContentView() {
		if (mContentViewBig == null) {
			mContentViewBig = new RemoteViews(getPackageName(), R.layout.remote_view_music_player);
			setUpRemoteView(mContentViewBig);
		}
		updateRemoteViews(mContentViewBig);
		return mContentViewBig;
	}

	private void setUpRemoteView(RemoteViews remoteView) {
		remoteView.setImageViewResource(R.id.image_view_close, R.drawable.ic_remote_view_close);
		remoteView.setImageViewResource(R.id.image_view_play_last, R.drawable.ic_remote_view_play_last);
		remoteView.setImageViewResource(R.id.image_view_play_next, R.drawable.ic_remote_view_play_next);

		remoteView.setOnClickPendingIntent(R.id.button_close, getPendingIntent(ACTION_STOP_SERVICE));
		remoteView.setOnClickPendingIntent(R.id.button_play_last, getPendingIntent(ACTION_PLAY_LAST));
		remoteView.setOnClickPendingIntent(R.id.button_play_next, getPendingIntent(ACTION_PLAY_NEXT));
		remoteView.setOnClickPendingIntent(R.id.button_play_toggle, getPendingIntent(ACTION_PLAY_TOGGLE));
	}

	private void updateRemoteViews(RemoteViews remoteView) {
		remoteView.setTextViewText(R.id.text_view_name, "从前慢");
		remoteView.setTextViewText(R.id.text_view_artist, "怀旧金曲");
		remoteView.setImageViewResource(R.id.image_view_play_toggle, R.drawable.ic_remote_view_play);
		remoteView.setImageViewResource(R.id.image_view_album, R.mipmap.ic_launcher);
	}

	// PendingIntent
	private PendingIntent getPendingIntent(String action) {
		return PendingIntent.getService(this, 0, new Intent(action), 0);
	}
}
